package com.excel.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;


import com.excel.entity.Student;
import com.excel.util.ExcelUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author linguochao
 * @Date 2020/9/10 17:55
 */
@RestController
public class ExcelController {



    @RequestMapping("/expor")
    public String exporExcel(HttpServletResponse response) throws IOException {

        ExcelWriter writer = null;
        try( OutputStream outputStream = response.getOutputStream();
             ) {
            //添加响应头信息
            response.setHeader("Content-disposition", "attachment; filename=" + "catagory.xls");
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma", "No-cache");//设置头
            response.setHeader("Cache-Control", "no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头

            //实例化 ExcelWriter
            writer = new ExcelWriter(outputStream, ExcelTypeEnum.XLS, true);

            //实例化表单
            Sheet sheet = new Sheet(1, 0, Student.class);
            sheet.setSheetName("目录");

            //获取数据
     //       List<Student> list = studentJpa.findAll();
            List<Student> list = new ArrayList<Student>();
            //输出
            writer.write(list, sheet);
            writer.finish();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("/import")
    public String importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();

        //实例化实现了AnalysisEventListener接口的类
        ExcelUtil.ExcelListener listener = new ExcelUtil.ExcelListener();
        //传入参数
        ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, listener);
        //读取信息
        excelReader.read(new Sheet(1, 1, Student.class));

        //获取数据
        List<Object> list = listener.getDatas();


        List<Student> catagoryList = new ArrayList<Student>();


        //转换数据类型,并插入到数据库
        for (int i = 0; i < list.size(); i++) {
            Student catagory = (Student) list.get(i);
            catagoryList.add(catagory);
        }

        System.out.println("catagoryList" + catagoryList);
        return "success";
    }

    @RequestMapping("/import2")
    public String importExcel2(@RequestParam("file") MultipartFile file) {
        try (InputStream inputStream = file.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(inputStream);
        ) {
            EasyExcelFactory.readBySax(bis, new Sheet(1, 1, Student.class), new AnalysisEventListener() {
                List<Student> studentList = new ArrayList<>();

                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                    Student student = (Student) o;
                    studentList.add(student);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    System.out.println("studentList" + studentList);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
}
